<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hashing</title>
<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Chapter 1. Fusion 2.2">
<link rel="up" href="../operator.html" title="Operator">
<link rel="prev" href="comparison/greater_than_equal.html" title="greater than equal">
<link rel="next" href="../../container.html" title="Container">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="comparison/greater_than_equal.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../operator.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../../container.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="fusion.sequence.operator.hashing"></a><a class="link" href="hashing.html" title="Hashing">Hashing</a>
</h4></div></div></div>
<p>
          Automatically create a <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">hash</span></code>
          conforming <code class="computeroutput"><span class="identifier">hash_value</span></code> function.
        </p>
<h6>
<a name="fusion.sequence.operator.hashing.h0"></a>
          <span class="phrase"><a name="fusion.sequence.operator.hashing.synopsis"></a></span><a class="link" href="hashing.html#fusion.sequence.operator.hashing.synopsis">Synopsis</a>
        </h6>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Seq</span><span class="special">&gt;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span>
<span class="identifier">hash_value</span><span class="special">(</span><span class="identifier">Seq</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">seq</span><span class="special">);</span>
</pre>
<h6>
<a name="fusion.sequence.operator.hashing.h1"></a>
          <span class="phrase"><a name="fusion.sequence.operator.hashing.parameters"></a></span><a class="link" href="hashing.html#fusion.sequence.operator.hashing.parameters">Parameters</a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    Parameter
                  </p>
                </th>
<th>
                  <p>
                    Requirement
                  </p>
                </th>
<th>
                  <p>
                    Description
                  </p>
                </th>
</tr></thead>
<tbody><tr>
<td>
                  <p>
                    <code class="computeroutput"><span class="identifier">seq</span></code>
                  </p>
                </td>
<td>
                  <p>
                    Instance of <a class="link" href="../../sequence.html" title="Sequence">Sequence</a>
                  </p>
                </td>
<td>
                  <p>
                    <a class="link" href="../../sequence.html" title="Sequence">Sequence</a> to compute hash
                    value of
                  </p>
                </td>
</tr></tbody>
</table></div>
<p>
          <span class="bold"><strong>Return type</strong></span>: <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span></code>
        </p>
<p>
          <span class="bold"><strong>Requirements</strong></span>:
        </p>
<p>
          For each element <code class="computeroutput"><span class="identifier">e</span></code> in sequence
          <code class="computeroutput"><span class="identifier">seq</span></code>, <code class="computeroutput"><span class="identifier">hash_value</span><span class="special">(</span><span class="identifier">seq</span><span class="special">)</span></code> is a valid expression returning a type
          that is convertible to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span></code>.
        </p>
<p>
          <span class="bold"><strong>Semantics</strong></span>: Returns a combined hash value
          for all elements of <code class="computeroutput"><span class="identifier">seq</span></code>.
        </p>
<h6>
<a name="fusion.sequence.operator.hashing.h2"></a>
          <span class="phrase"><a name="fusion.sequence.operator.hashing.header"></a></span><a class="link" href="hashing.html#fusion.sequence.operator.hashing.header">Header</a>
        </h6>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fusion</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">/</span><span class="identifier">hash</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fusion</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">hash</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<h6>
<a name="fusion.sequence.operator.hashing.h3"></a>
          <span class="phrase"><a name="fusion.sequence.operator.hashing.example"></a></span><a class="link" href="hashing.html#fusion.sequence.operator.hashing.example">Example</a>
        </h6>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fusion</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">equal_to</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fusion</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">hash</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fusion</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">vector</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">unordered_map</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">()</span>
<span class="special">{</span>
    <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">char</span><span class="special">&gt;</span> <span class="identifier">Vec</span><span class="special">;</span>
    <span class="keyword">const</span> <span class="identifier">Vec</span> <span class="identifier">v</span> <span class="special">=</span> <span class="special">{</span><span class="number">42</span><span class="special">,</span> <span class="string">"Hello World"</span><span class="special">,</span> <span class="char">'t'</span><span class="special">};</span>
    <span class="comment">// Compute a hash value directly.</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"hash_value(v) = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">hash_value</span><span class="special">(</span><span class="identifier">v</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="char">'\n'</span><span class="special">;</span>
    <span class="comment">// Or use it to create an unordered_map.</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">unordered_map</span><span class="special">&lt;</span><span class="identifier">Vec</span><span class="special">,</span> <span class="keyword">bool</span><span class="special">&gt;</span> <span class="identifier">map</span><span class="special">;</span>
    <span class="identifier">map</span><span class="special">[</span><span class="identifier">v</span><span class="special">]</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">;</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">map</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">1</span> <span class="special">&amp;&amp;</span> <span class="identifier">map</span><span class="special">.</span><span class="identifier">count</span><span class="special">(</span><span class="identifier">v</span><span class="special">)</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span>
<span class="special">}</span>
</pre>
<h6>
<a name="fusion.sequence.operator.hashing.h4"></a>
          <span class="phrase"><a name="fusion.sequence.operator.hashing.example0"></a></span><a class="link" href="hashing.html#fusion.sequence.operator.hashing.example0">Example</a>
        </h6>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fusion</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">define_struct</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fusion</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">equal_to</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fusion</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">hash</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">unordered_set</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="comment">// We would like to define a struct that we can form unordered_sets of.</span>
<span class="identifier">BOOST_FUSION_DEFINE_STRUCT</span><span class="special">(</span>
    <span class="special">(</span><span class="identifier">demo</span><span class="special">),</span> <span class="identifier">Key</span><span class="special">,</span>
    <span class="special">(</span><span class="keyword">bool</span><span class="special">,</span> <span class="identifier">b</span><span class="special">)</span>
    <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="identifier">s</span><span class="special">)</span>
    <span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">i</span><span class="special">)</span>
<span class="special">)</span>

<span class="keyword">namespace</span> <span class="identifier">demo</span> <span class="special">{</span>
    <span class="comment">// Make operator== and hash_value ADL accessible.</span>
    <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fusion</span><span class="special">::</span><span class="keyword">operator</span><span class="special">==;</span>
    <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">hash_value</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">unordered_set</span><span class="special">&lt;</span><span class="identifier">demo</span><span class="special">::</span><span class="identifier">Key</span><span class="special">&gt;</span> <span class="identifier">Set</span><span class="special">;</span>
<span class="special">}</span>

<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">()</span>
<span class="special">{</span>
    <span class="identifier">demo</span><span class="special">::</span><span class="identifier">Set</span> <span class="identifier">set</span><span class="special">;</span>
    <span class="identifier">demo</span><span class="special">::</span><span class="identifier">Key</span> <span class="identifier">key</span><span class="special">;</span>
    <span class="identifier">assert</span><span class="special">(</span><span class="identifier">set</span><span class="special">.</span><span class="identifier">count</span><span class="special">(</span><span class="identifier">key</span><span class="special">)</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
<span class="special">}</span>
</pre>
<h6>
<a name="fusion.sequence.operator.hashing.h5"></a>
          <span class="phrase"><a name="fusion.sequence.operator.hashing.see_also"></a></span><a class="link" href="hashing.html#fusion.sequence.operator.hashing.see_also">See
          also</a>
        </h6>
<p>
          <a href="http://www.boost.org/doc/html/hash.html" target="_top">Boost.ContainerHash
          Library</a>
        </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2006, 2011, 2012 Joel de Guzman,
      Dan Marsden, Tobias Schwinger<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="comparison/greater_than_equal.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../operator.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../../container.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
